package com.example.hospitalmanagement.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.bean.DiagnosisAndTreatmentItem

/**
 * 医院综合页面中间医院介绍+ 科室介绍+专家介绍三个视图所用适配器
 */
class HomeFragmentAdapter(context: Context) :
    RecyclerView.Adapter<HomeFragmentAdapter.HomeHolder>() {
    private val context = context
    private lateinit var listener: (position: Int) -> Unit
    fun setListener(listener: (position: Int) -> Unit) {
        this.listener = listener
    }

    lateinit var dataList: MutableList<DiagnosisAndTreatmentItem>

    class HomeHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val homeIcon: ImageView = itemView.findViewById(R.id.home_item_icon)
        val homeName: TextView = itemView.findViewById(R.id.home_item_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeHolder {
        val homeHolder = HomeHolder(
            LayoutInflater.from(context).inflate(R.layout.home_large_icon_item, parent, false)
        )
        homeHolder.itemView.setOnClickListener {
            listener(homeHolder.adapterPosition)
        }
        return homeHolder
    }

    override fun onBindViewHolder(holder: HomeHolder, position: Int) {
        holder.homeIcon.setImageResource(dataList[position].itemIcon)
        holder.homeName.text = dataList[position].itemName
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}